package dao;

import db.DBUtil;
import model.Boss;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 老板数据访问类，操作老板数据库
 */
public class BossDAO {
    
    /**
     * 老板登录
     */
    public Boss login(String bossId, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Boss boss = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM boss WHERE boss_id = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, bossId);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                boss = new Boss();
                boss.setBossId(rs.getString("boss_id"));
                boss.setName(rs.getString("name"));
                boss.setNetProfit(rs.getDouble("net_profit"));
                boss.setMemberCount(rs.getInt("member_count"));
                boss.setPhone(rs.getString("phone"));
                boss.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            System.err.println("老板登录查询失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        return boss;
    }
    
    /**
     * 更新净利润
     */
    public boolean updateNetProfit(String bossId, double amount) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE boss SET net_profit = net_profit + ? WHERE boss_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setDouble(1, amount);
            pstmt.setString(2, bossId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("更新净利润失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 更新会员数量
     */
    public boolean updateMemberCount(String bossId, int count) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE boss SET member_count = member_count + ? WHERE boss_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, count);
            pstmt.setString(2, bossId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("更新会员数量失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
    